package com.example.month11.festivalsms.db;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.example.month11.festivalsms.bean.SendedMsg;


// 已发送短信，数据库监听，
public class SmsProvider extends ContentProvider {
    public static final String AUTHORITY = "com.imooc.sms.provider.SmsProvider";
    public static final Uri URI_SMS_ALL = Uri.parse("content://" + AUTHORITY + "/sms");

    private static UriMatcher matcher;
    private static final int SMS_ALL = 0; // 所有
    private static final int SMS_ONE = 1; // 单个

    static {
        // 初始化matcher
        matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(AUTHORITY, "sms", SMS_ALL);
        matcher.addURI(AUTHORITY, "sms/#", SMS_ONE); // 单个短信
    }

    // 数据库操作类
    private SmsDbOpenHelper mHelper;
    private SQLiteDatabase mDb;

    @Override
    public boolean onCreate() {
        mHelper = SmsDbOpenHelper.getInstance(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        int match = matcher.match(uri);
        switch (match) {
            case SMS_ALL: // 查询多个

                break;
            case SMS_ONE: // 查询单个
                long id = ContentUris.parseId(uri);
                selection = "_id = ?";
                selectionArgs = new String[]{String.valueOf(id)}; // 查询数组拼装
                break;
            default:
                throw new IllegalArgumentException("Wrong URI:" + uri);
        }
        mDb = mHelper.getReadableDatabase();
        Cursor c = mDb.query(SendedMsg.TAB_NAME, projection,
                selection, selectionArgs, null, null, sortOrder);
        // setNotificationUri
        c.setNotificationUri(getContext().getContentResolver(), URI_SMS_ALL);
        return c;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        int match = matcher.match(uri);
        if (match != SMS_ALL) {
            throw new IllegalArgumentException("Wrong URI:" + uri);
        }
        mDb = mHelper.getWritableDatabase();
        long rowId = mDb.insert(SendedMsg.TAB_NAME, null, values);
        if (rowId > 0) {
            notifyDataSetChanged();
            return ContentUris.withAppendedId(uri, rowId);
        }
        return null;
    }

    private void notifyDataSetChanged() {
        // 通知发送
        getContext().getContentResolver().notifyChange(URI_SMS_ALL, null);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}
